package com.bys.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bys.exception.BysOaSystemException;
import com.bys.system.mapper.SysRoleMapper;
import com.bys.system.mapper.SysUserRoleMapper;
import com.bys.system.model.SysRoleModel;
import com.bys.system.model.SysUserModel;
import com.bys.system.service.ISysRoleService;
import com.bys.system.vo.CreateUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * PackageName:  com.bys.system.service.impl
 * ClassName:    SysRoleServiceImpl
 * Description:  角色 服务实现类
 * Author:       bys
 * Date:         2023-03-31
 **/
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleModel> implements ISysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public Page<SysRoleModel> getRolePage(Integer pageNo, Integer pageSize, SysRoleModel sysRoleModel) {
        Page<SysRoleModel> page = new Page<>(pageNo, pageSize);
        return sysRoleMapper.selectRolePage(page, sysRoleModel);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRoleLogic(Long id,Long updateUser) {
        SysRoleModel roleModel = sysRoleMapper.selectById(id);
        Optional.ofNullable(roleModel).orElseThrow(() -> new BysOaSystemException("角色不存在"));
        roleModel.setIsDeleted(Boolean.TRUE);
        roleModel.setUpdateTime(LocalDateTime.now());
        roleModel.setUpdateUser(updateUser);
        sysRoleMapper.update(roleModel, lambdaUpdate().getWrapper().eq(SysRoleModel::getId,roleModel.getId()));
    }
}
